package toanstt.com.MatchUp;

import java.util.HashMap;

import toanstt.com.MatchUp.R;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

public class Sound2
{
	static public int BUTTON_CLICK = 1;
	static public int CELL_CLICK = 2;
	static public int WRONG = 3;
	static public int WIN = 4;
	static public int RIGHT = 5;
	static public int FAIL = 6;
	static public int SLIPALL = 7;
	static public int SLIPUP = 8;
	
	private  SoundPool mSoundPool;
	private  HashMap<Integer, Integer> mSoundPoolMap;
	AudioManager  mAudioManager;
	private static  Context mContext;
	float currentVolume;
	static int c;
	private AudioManager aMgr;
	public Sound2()
	{
		initSounds();
	}
	public  void initSounds()
	{
		mContext = MainActivity.context;
		aMgr = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
		  mSoundPool = new SoundPool(16, AudioManager.STREAM_MUSIC, 0);
		  mSoundPoolMap = new HashMap<Integer, Integer>();
		  AddSounds();
		  //currentVolume = ((float)aMgr.getStreamVolume(AudioManager.STREAM_MUSIC)) / 
					// ((float)aMgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
		  Setvolum();
	}
	public  void addSound(Context mContext, int index, int SoundID)
	{
		
			  mSoundPoolMap.put(index, mSoundPool.load(mContext, SoundID, 1));
			
		//int k = mSoundPoolMap.put(index, mSoundPool.load(mContext, SoundID, 1)); 
		//Log.d("okddd",""+k);
	}
	public  void Play(int index) 
	{		
		  mSoundPool.play((Integer) mSoundPoolMap.get(index), currentVolume,currentVolume, 1, 0, 1.0f);
		 // mSoundPool.play(0,0.9f,0.9f, 1, 10, 1.0f);
	}
	public  void Stop(int index) 
	{		
		  //mSoundPool.play((Integer) mSoundPoolMap.get(index), currentVolume,currentVolume, 1, 0, 1.0f);
		  mSoundPool.stop((Integer) mSoundPoolMap.get(index));
		 // mSoundPool.play(0,0.9f,0.9f, 1, 10, 1.0f);
	}
	public  void PlayLoop(int index) 
	{		
		mSoundPool.play((Integer) mSoundPoolMap.get(index), currentVolume,currentVolume, 1, 100, 1.0f);
		  //mSoundPool.play((Integer) mSoundPoolMap.get(index), currentVolume,currentVolume, 1000, 1, 1.0f);
		  
		 // mSoundPool.play(0,0.9f,0.9f, 1, 10, 1.0f);
	}
	public  void AddSounds()
	{
		
		addSound(mContext,BUTTON_CLICK,R.raw.button_click);
		addSound(mContext,CELL_CLICK,R.raw.selectcell);
		addSound(mContext,WRONG,R.raw.wrong);
		addSound(mContext,WIN,R.raw.game_win);
		addSound(mContext,RIGHT,R.raw.right);
		addSound(mContext,FAIL,R.raw.fail);
		addSound(mContext,SLIPALL,R.raw.slipall);
		addSound(mContext,SLIPUP,R.raw.slipup);
		
	}
	public void Exit()
	{
		//mSoundPool.stop(THEME);
		
	}
	public void Setvolum()
	{
		float a = T.SOUND*1.0f/5;
		if(a == 0.2f)
			a = 0.15f;
		else if(a == 0.4f)
			a = 0.25f;
		else if(a == 0.6f)
			a = 0.45f;
		currentVolume = ((float)(a*aMgr.getStreamVolume(AudioManager.STREAM_MUSIC))) / 
				 ((float)(aMgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC)));
	}
//	public void onCreate(Bundle savedInstanceState)
//	{
//        super.onCreate(savedInstanceState);
// youraudioclass.initSounds();
// youraudioclass.addSound(this, 1, r.raw.sound1);
// youraudioclass.addSound(this, 2, r.raw.sound2);
// youraudioclass.addSound(this, 2, r.raw.boom);
//  //and so on, just stick the mp3/wav/whatever in a res/raw folder.
//  //the number is just for your own reference for when you want to play the sound
//}
}
